//
//  HsNumberUtil.m
//  Hybrid-iPad
//
//  Created by zhaoyk on 14-5-21.
//  Copyright (c) 2014年 hundsun. All rights reserved.
//

#import "HsNumberUtil.h"

@implementation HsNumberUtil

+(NSString *)numberFormattingByThree:(NSNumber*)number{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;//123456789----123,456,789
    NSString *string = [formatter stringFromNumber:number];
//    NSLog(@"Formatted number string:%@",string);
    return string;
}

+(NSString *)numberMBillionByThree:(NSNumber*)number{
    //万 亿 万亿
    NSArray* mbillionArr = @[@"万",@"亿",@"万亿"];
    NSString* numberStr = nil;
    int i = 0;
    double dnumber = [number doubleValue];
    while (dnumber > 10000.0 && i < mbillionArr.count) {
        if(dnumber/10000-(int)(dnumber/10000)>0)break;
        ++i;
        dnumber /= 10000.0;
    }
    
    if (i != 0) {
//        if(dnumber-(int)dnumber>0){
//            return [self numberFormattingByThree:number];
//        }
//        NSString* formatStr = [[NSString alloc] initWithFormat:@"%d",(int)dnumber];
        NSString* formatStr = [HsNumberUtil numberFormattingByThree:@((int)dnumber)];
        numberStr = [formatStr stringByAppendingString:[mbillionArr objectAtIndex:(i - 1)]];
    }else{
        numberStr = [HsNumberUtil numberFormattingByThree:@(dnumber)];
        //[[NSString alloc] initWithFormat:@"%0.0f",[number doubleValue]];
    }
    
    return numberStr;
}


@end
